<template>
  <view class="viewer-container">
    <view class="default" v-if="spectatorList.length === 0">
      提前添加观影人信息,购票永远快人一步~
    </view>
    <view class="list" v-else>
      <scroll-view class="scroll" scroll-y="true" scroll-with-animation="true">
        <view class="item" v-for="(item,index) in spectatorList" :key="index">
          <view class="left"  @click="toUpdate(item)">
            <view>
              {{ item.spectatorName }}
            </view>
            <view>
              <span style="font-size: 13px;">身份证</span><span class="number"> {{
                ' ' + item.certificateNumber
              }}</span>
            </view>
            <view>
              <span style="font-size: 13px;">手机号</span> <span class="number"> {{
                ' ' + item.spectatorPhoneNumber
              }}</span>
            </view>
          </view>
          <view @click="doSelectDelete(item)" class="right">
            删除
          </view>
        </view>
        <uni-popup ref="warning" type="dialog">
          <uni-popup-dialog :type="msgType" cancelText="取消" confirmText="确定" title="警告"
                            :content="'是否删除观影者【'+deletedSpectator.spectatorName+'】'"
                            @confirm="doDelete"></uni-popup-dialog>
        </uni-popup>
      </scroll-view>
    </view>

    <view class="addBox">
      <view class="addBtn" @click="toAdd"> +新增观影人</view>
    </view>
  </view>
</template>

<script>
import {getSpectatorList, removeSpectator} from "../../api/user/spectator";

export default {
  data() {
    return {
      spectatorList: [],
      deletedSpectator: null,
    };
  },
  onShow() {
    this.getList();
  },
  methods: {
    toAdd() {
      uni.navigateTo({
        url: "/subpkg/add-viewer/add-viewer",
        fail(error) {
          console.log(error);
        },
      });
    },
    doSelectDelete(item) {
      this.deletedSpectator = item;
      this.$refs.warning.open();
    },
    doDelete() {
      removeSpectator(this.deletedSpectator.spectatorId).then(res => {
        if (res.success) {
          this.getList();
          uni.showToast({
            title: '删除成功!',
            icon: 'success',
            duration: 1000,
          })
        }
      })
    },
    getList() {
      getSpectatorList().then(res => {
        this.spectatorList = res.data;
      })
    },
    toUpdate(item) {
      uni.navigateTo({
        url: `/subpkg/add-viewer/add-viewer?spectatorId=${item.spectatorId}`,
        fail(error) {
          console.log(error);
        },
      });
    }
  },
};
</script>

<style lang="scss">
.viewer-container {
  width: 100%;
  height: 100vh;
  background-color: #e9e4e4;
}

.list {
  height: calc(100% - 130upx);
  background-color: white;

  .scroll {
    padding: 0 10px;
    height: 100%;

  }

  .item {
    border-bottom: 1px solid #e5e5e5;
    margin-bottom: 10px;
    padding: 10px 10px;
    display: flex;
    align-items: center; /* Align children vertically */

    .left {
      margin-left: 10px;
      width: 80%;

      .number {
        font-size: 12px;
      }
    }

    .right {
      width: 20%;
      font-size: 13px;
      text-align: center;
      color: #FC1B4A;
    }
  }
}


.default {
  position: relative;
  left: 31%;
  top: 42%;
  width: 36%;
  font-size: 14px;
  color: #646363;
}

.addBox {
  position: fixed;
  display: flex;
  left: 0;
  bottom: 0;
  z-index: 99;
  width: 100%;
  height: 130upx;
  background-color: #ffffff;
  align-items: center;
  justify-content: center;
  border-top: 1px solid #e5e5e5;

  .addBtn {
    margin-bottom: 10px;
    height: 70%;
    width: 80%;
    background: linear-gradient(to right, #FF7E81, #FF2E79);
    border-radius: 24px;
    text-align: center;
    color: white;
    line-height: 90upx;
  }
}
</style>
